/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.performance;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.bean.Page;
import com.sinodata.bsm.center.bean.PropValueBean;
import com.sinodata.bsm.center.bean.PropertyBean;
import com.sinodata.bsm.center.bean.ResBean;
import com.sinodata.bsm.center.bean.ResTypeBean;
import com.sinodata.bsm.center.cache.PropertyCache;
import com.sinodata.bsm.center.cache.PropertyValueCache;
import com.sinodata.bsm.center.cache.ResAttrValCache;
import com.sinodata.bsm.center.service.performance.PropValueService;
import com.sinodata.bsm.center.service.performance.RealTimeService;
import com.sinodata.bsm.center.service.resource.ResService;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.constants.DataTypeConstants;
import com.sinodata.bsm.common.vo.Property;
import com.sinodata.bsm.common.vo.PropertyValue;
import com.sinodata.bsm.common.vo.RealTimeViewConf;
import com.sinodata.bsm.common.vo.Res;
import com.sinodata.bsm.common.vo.ResAttrVal;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * Jul 3, 2012 11:20:10 AM          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@SuppressWarnings("rawtypes")
public class RealTimeAction extends BaseAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private static Logger logger = Logger.getLogger(RealTimeAction.class);

    private final SimpleDateFormat hourSdf = new SimpleDateFormat("HH");

    private Long id;

    private Res entity;

    @Autowired
    private ResService resService;

    @Autowired
    private PropertyCache propertyCache;

    @Autowired
    private RealTimeService realTimeService;

    @Autowired
    private PropertyValueCache propertyValueCache;

    @Autowired
    private PropValueService propService;

    @Autowired
    private ResAttrValCache resAttrValCache;

    /**
     * 进入  我的视图-性能页面  根据不同的返回结果进入不同的页面
     */
    public String common() {

        entity = resService.get(id);

        request.setAttribute("res", entity);
        request.setAttribute("resTypeId", entity.getResTypeId());
        request.setAttribute("id", id);
        request.setAttribute("pageId", "default");

        return "common";
    }

    public String chart() {
        entity = resService.get(id);
        RealTimeViewConf realConf = realTimeService.findRealTimeView(id);
        if (realConf == null) {
            logger.info(" no chart!"); //表明此类别没有图表的页面
            request.setAttribute("chartpage", true);
            return "nopage";
        }
        request.setAttribute("res", entity);
        request.setAttribute("resTypeId", entity.getResTypeId());
        request.setAttribute("id", id);
        request.setAttribute("pageId", "default");
        return "chart";
    }

    public String combine() {
        entity = resService.get(id);
        String pageConf = realTimeService.findPageByResType(entity.getResTypeId());
        if (pageConf == null) {
            logger.info(" no combine page!"); //表明此类别没有图表的页面
            request.setAttribute("chartpage", false);
            return "nopage";
        }
        request.setAttribute("res", entity);
        request.setAttribute("resTypeId", entity.getResTypeId());
        request.setAttribute("id", id);
        request.setAttribute("pageId", "default");
        return pageConf;
    }

    public String getRes() {
        String resId = request.getParameter("resId");
        Res res = ResBean.get(Long.parseLong(resId)).res();
        renderText(res.toString());
        return null;
    }

    public String getResType() {
        String resId = request.getParameter("resId");
        Res res = ResBean.get(Long.parseLong(resId)).res();
        renderText(res.getResTypeId().toString());
        return null;
    }

    public String savePerfMenu() {
        String perfid = request.getParameter("perfid");
        request.getSession().setAttribute("perfid", perfid);
        return null;
    }

    /**
     * 获取一个资源所有的数值类型的指标
     * @param resId 父资源的ID
     * @param childType 子资源的类别      此类别的资源应该只有一个   即：在发现程序中直接写定
     * @return
     */
    public String getNumPropList() {
        String resId = request.getParameter("resId");
        String childType = request.getParameter("childType");
        try {
            ResBean rbean = null;
            if (childType != null && childType.length() > 0) {
                ResBean[] beans = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
                if (beans.length > 0) {
                    rbean = ResBean.get(beans[0].res().getId());
                } else {
                    return null;
                }
            }
            ResTypeBean bean = ResTypeBean.get(rbean.res().getResTypeId());
            if (bean == null) {
                return null;
            }
            List<Property> properties = bean.properties();

            JSONArray propArray = new JSONArray();
            for (Property p : properties) {
                if (p.getDataTypeId().intValue() == DataTypeConstants.DATA_TYPE_NUM) {
                    JSONObject json = new JSONObject();
                    json.element("id", p.getId());
                    json.element("name", p.getName());
                    propArray.add(json);
                } else {
                    continue;
                }
            }
            Struts2Utils.renderText(propArray.toString());
        } catch (Exception e) {
            logger.error(e);
        }

        return null;
    }

    /**
     * 生成一个特定资源 特定指标的性能曲线数据
     * @return
     */
    public String propCurve() {
        try {
            String resid = request.getParameter("resId");
            String props = request.getParameter("propIds");
            String childType = request.getParameter("childType");
            if (childType != null && childType.length() > 0) {
                ResBean[] beans = ResBean.get(Long.parseLong(resid)).childrenByType(Long.parseLong(childType));
                if (beans.length > 0) {
                    resid = beans[0].res().getId() + "";
                } else {
                    resid = "-1";
                    return null;
                }
            }

            String[] propArry = null;
            if (props != null && props.length() > 0) {
                propArry = props.split(",");
            }
            int[] propids = new int[propArry.length];
            for (int i = 0; i < propArry.length; i++) {
                Property p = propertyCache.get(Long.parseLong(propArry[i]));
                int ii = p.getStore();
                propids[i] = ii;
            }

            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
            Calendar now = Calendar.getInstance();
            String startDate = sf.format(now.getTime()) + "000000";

            List<PropertyValue> list = propService.getPropValues(Long.parseLong(resid), propids, "20120705010101", "", "0");
            if (list == null || list.size() < 2) {
                now.add(Calendar.DATE, -1); //从前一天开始取性能指标数据
                startDate = sf.format(now.getTime()) + "000000";
                list = propService.getPropValues(Long.parseLong(resid), propids, startDate, "", "1");
            }

            JSONObject json = buildJsonFor2Line(list, propArry, null);

            //  System.out.println(json.toString());
            renderText(json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成二维线图
     * @return
     */
    public String get2LineChart() {
        String resid = request.getParameter("resId");
        String props = request.getParameter("propIds");
        String childType = request.getParameter("childType");
        String aliasName = request.getParameter("alisname");
        try {
            if (childType != null && childType.length() > 0) {
                ResBean[] beans = ResBean.get(Long.parseLong(resid)).childrenByType(Long.parseLong(childType));
                if (beans.length > 0) {
                    resid = beans[0].res().getId() + "";
                } else {
                    resid = "-1";
                    return null;
                }
            }

            String[] names = null;
            if (aliasName != null && aliasName.length() > 0) {
                names = aliasName.split("[,]");
            }

            String[] propArry = null;
            if (props != null && props.length() > 0) {
                propArry = props.split(",");
            }
            int[] storeOrder = new int[propArry.length];
            for (int i = 0; i < propArry.length; i++) {
                Property p = propertyCache.get(Long.parseLong(propArry[i]));
                int ii = p.getStore();
                storeOrder[i] = ii;
            }

            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
            Calendar now = Calendar.getInstance();
            String startDate = sf.format(now.getTime()) + "000000";
            List<PropertyValue> list = propService.getTrendPropValus(Long.parseLong(resid), storeOrder, startDate, "", "0", "1");

            JSONObject json = buildJsonFor2Line(list, propArry, names);

            Struts2Utils.renderText(json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String get2LineChart2() {
        String resid = request.getParameter("resId");
        String props = request.getParameter("propIds");
        try {
            String[] resIdArray = resid.split(",");

            String[] propArry = null;
            if (props != null && props.length() > 0) {
                propArry = props.split(",");
            }
            int[] storeOrder = new int[propArry.length];
            for (int i = 0; i < propArry.length; i++) {
                Property p = propertyCache.get(Long.parseLong(propArry[i]));
                int ii = p.getStore();
                storeOrder[i] = ii;
            }

            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
            Calendar now = Calendar.getInstance();
            String startDate = sf.format(now.getTime()) + "000000";
            List<PropertyValue> list = new ArrayList<PropertyValue>();
            for (String id : resIdArray) {
                list.addAll(propService.getTrendPropValus(Long.parseLong(id), storeOrder, startDate, "", "0", "1"));
            }

            JSONObject json = buildJsonFor2Line(list, propArry, null);
            Struts2Utils.renderText(json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 为二维线图构建JSON数据
     * @param list
     * @param propArry
     * @return
     */
    private JSONObject buildJsonFor2Line(List<PropertyValue> list, String[] propArry, String[] aliasNames) {
        JSONObject json = new JSONObject();
        JSONObject chart = new JSONObject();
        chart.element("showvalues", "0");
        //chart.element("linethickness", "2");
        chart.element("showLabels ", "1");
        chart.element("numvdivlines", "4");
        chart.element("slantLabels", "1");
        //        chart.element("anchorradius", "2");
        chart.element("rotateLabels", "1");
        //        chart.element("useEllipsesWhenOverflow ", "1");
        //        chart.element("labelDisplay ", "STAGGER");
        //        chart.element("adjustDiv ", "1");

        chart.element("canvasbgcolor", "FFFFFF");
        chart.element("canvasbgangle", "270");
        chart.element("alternateHGridColor", "FFFFFF");
        chart.element("canvasBorderThickness", "0");
        chart.element("canvasBorderColor", "f1f1f1");
        chart.element("baseFontColor", "1D8BD1");
        chart.element("plotGradientColor", "DCE6F9");
        chart.element("divlinecolor", "FFFFFF");
        chart.element("divlinealpha", "60");
        chart.element("chartBottomMargin", "-5");

        //        chart.element("labelStep", "1");

        chart.element("bgcolor", "FFFFFF");
        chart.element("bordercolor", "FFFFFF");
        chart.element("showlegend", "0");
        //chart.element("baseFontSize", "12");
        chart.element("yAxisMaxValue", "100");

        //chart.element("numbersuffix", PropertyBean.get(Long.parseLong(propArry[0])).property().getDataUnit());

        //计算步长
        int step = list.size() / 10 + 1;
        chart.element("labelStep", step);

        JSONArray jj = new JSONArray();
        JSONObject o = new JSONObject();
        JSONArray labelJson = new JSONArray();
        for (PropertyValue p : list) {
            JSONObject obj = new JSONObject();
            //            obj.element("label", p.getTime().toString().substring(11, p.getTime().toString().length() - 8));
            obj.element("label", hourSdf.format(p.getTime()));
            labelJson.add(obj);
        }
        o.element("category", labelJson);
        jj.add(o);

        JSONArray dataset = new JSONArray();
        if (aliasNames != null && aliasNames.length == propArry.length) { //是否显示别名
            for (int j = 0; j < propArry.length; j++) {
                JSONObject obj = new JSONObject();
                obj.element("seriesname", aliasNames[j]); //曲线名称
                JSONArray ja = new JSONArray();
                for (PropertyValue p : list) {
                    JSONObject n = new JSONObject();
                    n.element("value", "".equals(p.getValues()[j]) ? "0" : p.getValues()[j]);
                    ja.add(n);
                }
                obj.element("data", ja);
                dataset.add(obj);
            }
        } else {
            for (int j = 0; j < propArry.length; j++) {
                JSONObject obj = new JSONObject();
                Property pt = PropertyBean.get(Long.parseLong(propArry[j])).property();
                String unit = (pt.getDataUnit() == null || pt.getDataUnit().length() == 0) ? "" : "(" + pt.getDataUnit() + ")";
                obj.element("seriesname", pt.getName() + unit); //曲线名称
                JSONArray ja = new JSONArray();
                for (PropertyValue p : list) {
                    JSONObject n = new JSONObject();
                    n.element("value", "".equals(p.getValues()[j]) ? "0" : p.getValues()[j]);
                    ja.add(n);
                }
                obj.element("data", ja);
                dataset.add(obj);
            }
        }

        json.element("categories", jj);
        json.element("chart", chart);
        json.element("dataset", dataset);
        return json;

    }

    /**
     * 为多个子资源的同一个指标(百分比)生成柱图        并可以排序
     * @return
     */
    public String get2ColumnChart() {
        String resId = request.getParameter("resId");
        String childType = request.getParameter("childType");
        String propId = request.getParameter("propIds");
        //String detail = request.getParameter("detail");
        String top = request.getParameter("top");
        String unit = request.getParameter("unit");
        ResBean[] resBean;
        try {
            if (childType != null && childType.length() > 0) {
                resBean = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
            } else {
                ResBean rb = ResBean.get(Long.parseLong(resId));
                resBean = new ResBean[1];
                resBean[0] = rb;
            }
            JSONObject json = buildJsonFor2Column(unit, resBean, propId, Integer.parseInt(top));

            Struts2Utils.renderText(json.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 为百分比指标生成  柱图  排序 
     * @param detail  是否显示 '%'
     * @param resBean 资源
     * @param propIds 指标ID  从指标缓存中获取值
     * @param top     Top数
     * @return
     */
    @SuppressWarnings("unchecked")
    private JSONObject buildJsonFor2Column(String unit, ResBean[] resBean, String propId, int top) {
        JSONObject json = new JSONObject();
        JSONObject chart = new JSONObject();

        //chart.element("canvasbgalpha", "0");
        chart.element("bgAlpha", "0");
        chart.element("showPlotBorder", "1");
        chart.element("showValues", "1");
        chart.element("canvasBorderThickness", "1");
        chart.element("numVDivLines", "10");
        chart.element("bgcolor", "FFFFFF");
        chart.element("bordercolor", "FFFFFF");
        chart.element("showlabels", "1");
        chart.element("showShadow", "1");
        chart.element("showlegend", "0");
        chart.element("useRoundEdges", "1");
        chart.element("showBorder", "1");
        chart.element("showShadow", "1");
        chart.element("chartBottomMargin", "3");
        chart.element("chartTopMargin", "5");

        if ("%".equals(unit)) {
            chart.element("yAxisMaxValue", "100");
        }

        chart.element("numbersuffix", null == unit ? "" : unit);

        List<FileSystem> fsList = new ArrayList<FileSystem>();
        for (ResBean rb : resBean) {
            String value = propertyValueCache.getValue(rb.res().getId(), Long.parseLong(propId));
            value = value == null || value.equals("") ? "0" : value;
            FileSystem sj = new FileSystem(rb.res().getName(), Double.parseDouble(value));
            fsList.add(sj);
        }

        Collections.sort(fsList, new FSSort());

        JSONArray data = new JSONArray();
        for (int i = 0; i < (top > fsList.size() ? fsList.size() : top); i++) {
            JSONObject v = new JSONObject();
            FileSystem fs = fsList.get(i);
            v.element("label", fs.getName());
            v.element("value", fs.getValue());
            data.add(v);
        }
        json.element("chart", chart);
        json.element("data", data);
        return json;
    }

    /**
     * 获取资源的基本信息  在综合性能页面的左侧显示  
     * @return
     */
    public String getBaseInfo() {
        String resId = request.getParameter("resId");
        String propIds = request.getParameter("propIds");
        String childType = request.getParameter("childType");
        try {

            if (childType != null && childType.length() > 0) {
                ResBean[] beans = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
                if (beans.length > 0) {
                    resId = beans[0].res().getId() + "";
                } else {
                    resId = "-1";
                    return null;
                }
            }

            String[] tmp = propIds.split("[,]");
            Res res = ResBean.get(Long.parseLong(resId)).res();
            JSONObject json = new JSONObject();
            json.element("name", res.getName());
            json.element("ip", res.getIp());
            for (String t : tmp) {
                json.element(t, getValue(Long.parseLong(resId), t));
            }
            Struts2Utils.renderText(json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 饼图
     * @return
     */
    public String getPieChart() {
        String resId = request.getParameter("resId");
        String porps = request.getParameter("propIds");
        String[] tmp = porps.split(",");
        JSONObject json = buildJsonForPie(Long.parseLong(resId), tmp);

        ///System.out.println(json.toString());
        Struts2Utils.renderText(json.toString());
        return null;
    }

    /**
     * 为饼图构建JSON对象
     * @param propIds
     * @return
     */
    private JSONObject buildJsonForPie(Long resId, String[] propIds) {
        JSONObject json = new JSONObject();
        JSONObject chart = new JSONObject();
        chart.element("showlabels", "0");
        chart.element("manageResize", "1");
        chart.element("showvalues", "0");
        chart.element("showlegend", "0");
        chart.element("bgcolor", "FFFFFF");
        chart.element("bgalpha", "70");
        chart.element("showpercentvalues", "1");
        chart.element("bordercolor", "FFFFFF");
        chart.element("animation", "0");
        JSONArray data = new JSONArray();
        for (int i = 0; i < propIds.length; i++) {
            JSONObject obj = new JSONObject();
            Property p = PropertyBean.get(Long.parseLong(propIds[i])).property();
            if (p == null) {
                continue;
            }
            obj.element("label", p.getName());
            obj.element("value", propertyValueCache.getValue(resId, Long.parseLong(propIds[i])));
            data.add(obj);
        }

        json.element("data", data);
        json.element("chart", chart);
        return json;
    }

    /**
     * 获取一个实值指标值
     * @return 实时指标值结果   百分比
     */
    public String getRealTimeVal() {
        String resid = request.getParameter("resId");
        String propid = request.getParameter("propid");
        try {
            String[] tmp = propid.split("_");
            String value = "0";
            if (tmp.length == 2) { //子资源的指标值
                ResBean[] childs = ResBean.get(Long.parseLong(resid)).childrenByType(Long.parseLong(tmp[0]));
                if (childs.length > 0) {
                    value = propertyValueCache.getValue(childs[0].res().getId(), Long.parseLong(tmp[1]));
                }
            } else {//资源的指标值
                String[] cpus = propid.split("[,]");
                int v = 0;
                for (String cpu : cpus) {
                    String midv = propertyValueCache.getValue(Long.parseLong(resid), Long.parseLong(cpu));
                    v += (midv == null) ? 0 : Integer.parseInt(midv);
                }
                value = v + "";
            }

            if (value == null || value.length() == 0) {
                value = "0";
            }
            // System.out.println("value==" + value);
            Struts2Utils.renderText(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取一个资源 一个实时指标值
     * @return 
     */
    public String getRate() {
        String resid = request.getParameter("resId");
        String propid = request.getParameter("propid");
        String detail = request.getParameter("detail");
        try {
            String[] tmp = propid.split("_");
            String value = "0";
            if (tmp.length == 2) { //子资源的指标值
                ResBean[] childs = ResBean.get(Long.parseLong(resid)).childrenByType(Long.parseLong(tmp[0]));
                if (childs.length > 0) {
                    value = propertyValueCache.getValue(childs[0].res().getId(), Long.parseLong(tmp[1]));
                }
            } else {//资源的指标值
                value = propertyValueCache.getValue(Long.parseLong(resid), Long.parseLong(propid));
            }
            if (value == null || value.length() == 0) {
                value = "0";
            }

            JSONObject json = buildJsonForRate(detail, value);
            //  System.out.println("value---" + value);
            renderText(json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取实时指标值  非百分比值
     * @return
     */
    public String getRealValue() {
        String resid = request.getParameter("resId");
        String propid = request.getParameter("propid");
        try {
            String[] tmp = propid.split("_");
            String value = "0";
            if (tmp.length == 2) {
                ResBean[] childs = ResBean.get(Long.parseLong(resid)).childrenByType(Long.parseLong(tmp[0]));
                if (childs.length > 0) {
                    value = propertyValueCache.getValue(childs[0].res().getId(), Long.parseLong(tmp[1]));
                }
            } else {
                value = propertyValueCache.getValue(Long.parseLong(resid), Long.parseLong(propid));
            }
            if (value == null || value.length() == 0) {
                value = "0";
            }
            JSONObject json = buildJsonForReal(value);
            renderText(json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private JSONObject buildJsonForReal(String value) {
        JSONObject json = new JSONObject();
        JSONObject chart = new JSONObject();
        chart.element("lowerlimit", "0");
        chart.element("chartLeftMargin", "0");
        chart.element("chartRightMargin", "0");
        chart.element("chartTopMargin", "0");
        chart.element("chartBottomMargin", "0");
        chart.element("showTickMarks", "0");
        chart.element("showTickValues", "0");
        chart.element("showlabels", "0");

        JSONObject colorrange = new JSONObject();
        JSONArray color = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.element("minvalue", "0");
        obj.element("maxvalue", "20");
        obj.element("code", "00dd00");
        color.add(obj);

        colorrange.element("color", color);
        json.element("colorrange", colorrange);
        json.element("value", value);
        json.element("chart", chart);
        return json;
    }

    private JSONObject buildJsonForRate(String detail, String value) {
        JSONObject json = new JSONObject();
        JSONObject chart = new JSONObject();
        if ("1".equals(detail)) {
            chart.element("manageresize", "1");
            chart.element("origw", "240");
            chart.element("origh", "250");
            chart.element("upperlimit", "100");
            chart.element("lowerlimit", "0");
            chart.element("numbersuffix", "%");
            chart.element("majortmnumber", "6");
            chart.element("majortmcolor", "646F8F");
            chart.element("majortmheight", "9");
            chart.element("minortmnumber", "2");
            chart.element("minortmcolor", "646F8F");
            chart.element("minortmheight", "3");
            chart.element("majortmthickness", "1");
            chart.element("decmials", "0");
            chart.element("ledgap", "2");
            chart.element("ledsize", "2");
            chart.element("annrenderdelay", "1.7");
            chart.element("bgcolor", "FFFFFF");
            chart.element("bordercolor", "FFFFFF");
            chart.element("showlabels", "0");
            chart.element("showTickMarks", "0");

            JSONObject annotations = new JSONObject();
            JSONArray groups = new JSONArray();
            JSONObject g = new JSONObject();
            g.element("id", "GRP1");
            g.element("showbelow", "0");
            g.element("x", "$gaugeCenterX");
            g.element("constrainedscale", "0");
            JSONArray items = new JSONArray();
            JSONObject it = new JSONObject();
            it.element("type", "line");
            it.element("y", "$gaugeStartY+1");
            it.element("toy", "$gaugeEndY-1");
            it.element("color", "000000");
            it.element("linethickness", "2");
            items.add(it);
            g.element("items", items);
            groups.add(g);
            annotations.element("groups", groups);
            json.element("annotations", annotations);
        } else {
            chart.element("upperlimit", "100");
            chart.element("lowerlimit", "0");
            chart.element("showTickMarks", "0");
            chart.element("showTickValues", "0");
            chart.element("chartLeftMargin", "1");
            chart.element("chartRightMargin", "1");
            chart.element("chartTopMargin", "1");
            chart.element("chartBottomMargin", "1");
            chart.element("showValue", "0");
            chart.element("numbersuffix", "%");
            chart.element("majortmthickness", "1");
            chart.element("decmials", "0");
            chart.element("annrenderdelay", "1.7");
            chart.element("ledsize", "2");
            // chart.element("manageresize", "1");
        }

        JSONObject colorrange = new JSONObject();
        JSONArray color = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.element("minvalue", "0");
        obj.element("maxvalue", "20");
        obj.element("code", "00dd00");
        color.add(obj);

        colorrange.element("color", color);
        json.element("colorrange", colorrange);
        json.element("value", value);
        json.element("chart", chart);
        //System.out.println(json.toString());
        return json;
    }

    /**
     * 为一个资源的不同指标生成二维柱状图
     * 如:负载
     * @return
     */
    public String get2ColumnPropChart() {
        String resId = request.getParameter("resId");
        String childType = request.getParameter("childType");
        String propIds = request.getParameter("propIds");
        String namealias = request.getParameter("namealias");
        String[] tmp = propIds.split(",");
        String[] names = namealias.split(",");
        try {

            if (childType != null && childType.length() > 0) {
                ResBean[] beans = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
                if (beans.length > 0) {
                    resId = beans[0].res().getId() + "";
                } else {
                    resId = "-1";
                    return null;
                }
            }
            JSONObject json = buildJsonForPropColumn(resId, tmp, names);
            //todo
            //System.out.println(json.toString());
            Struts2Utils.renderText(json.toString());
        } catch (Exception e) {
            logger.error(e);
        }
        return null;
    }

    /**
     * 
     * @return
     */
    private JSONObject buildJsonForPropColumn(String resId, String[] propids, String[] names) {
        JSONObject json = new JSONObject();
        JSONObject chart = new JSONObject();
        chart.element("bgAlpha", "0");
        chart.element("bgImageAlpha", "0");
        chart.element("showPlotBorder", "1");
        chart.element("showValues", "0");
        chart.element("canvasBorderThickness", "1");
        chart.element("numVDivLines", "10");
        chart.element("bgcolor", "FFFFFF");
        chart.element("bordercolor", "FFFFFF");
        chart.element("showlabels", "1");
        chart.element("useRoundEdges", "1");
        chart.element("showBorder", "0");
        chart.element("chartBottomMargin", "3");
        chart.element("chartTopMargin", "5");

        JSONArray data = new JSONArray();
        for (int p = 0; p < propids.length; p++) {
            JSONObject obj = new JSONObject();
            obj.element("label", names.length == propids.length ? names[p] : PropertyBean.get(Long.parseLong(propids[p])).property().getName());
            String value = propertyValueCache.getValue(Long.parseLong(resId), Long.parseLong(propids[p]));
            obj.element("value", value == null ? "0" : value);
            data.add(obj);
        }
        json.element("chart", chart);
        json.element("data", data);
        return json;

    }

    /**
     * 为子资源的  多个指标  生成柱图    可确定子资源只有一个 
     * @return
     */
    public String get2ColumnChildResChart() {

        return null;
    }

    /**
     * 多个资源获取相同指标值--主要用于esx的 虚拟机列表展示
     * @return
     */
    public String getResPropValue() {
        String[] res = Struts2Utils.getParameter("resId").split(",");
        String[] props = Struts2Utils.getParameter("propid").split(",");

        JSONArray resultArray = new JSONArray();
        for (String str : res) {
            JSONObject entity = new JSONObject();
            Res resOb = ResBean.get(Long.parseLong(str)).res();
            entity.element("id", resOb.getId());
            entity.element("name", resOb.getName());
            entity.element("runStatu", propertyValueCache.getValue(resOb.getId(), Long.parseLong(props[0])));
            entity.element("cpuCount", propertyValueCache.getValue(resOb.getId(), Long.parseLong(props[1])));
            entity.element("memorySize", propertyValueCache.getValue(resOb.getId(), Long.parseLong(props[2])));
            resultArray.add(entity);
        }

        Struts2Utils.renderJson(resultArray);
        return null;
    }

    /**
     * 查询日志类型指标  并组装成ErrorLogBean对象类
     * @return
     */
    public String getErrorLogList() {
        JSONObject json = new JSONObject();
        List<ErrorLogBean> errorlist = new ArrayList<ErrorLogBean>();
        String resId = request.getParameter("resId");

        int[] storeOrder = new int[1];
        storeOrder[0] = 10;

        Page<String[]> page = new Page<String[]>();
        page.setPageNo(Integer.parseInt(request.getParameter("page")));
        page.setPageSize(40);

        page = propService.getLogContentProps(Long.parseLong(resId), page, storeOrder, "20120801000000", "");
        List<String[]> list = page.getRows();

        for (String[] stry : list) {
            ErrorLogBean log = new ErrorLogBean();
            log.setFileName(stry[0]);
            log.setTime(stry[1]);
            log.setErrorFlag(resAttrValCache.get(Long.parseLong(resId), 120501020000003L).getAttrValue());
            log.setErrorContent(stry[2]);
            errorlist.add(log);
        }
        json.element("log", errorlist);
        json.element("total", 10);
        json.element("pageCnt", Math.ceil(page.getTotal() / 40.0));
        Struts2Utils.renderText(json.toString());
        return null;
    }

    /**
     * 构建资源 实时性能 指标数据dataunit为4(对象型)的datatables数据
     * @return
     */
    public String initDatatables() {
        PropertyValueCache cache = SpringContextHolder.getBean(PropertyValueCache.class);
        String resid = request.getParameter("resId");
        String childType = request.getParameter("childType");
        String propId = request.getParameter("propId");

        if (childType != null && childType.length() > 0) {
            ResBean[] beans = ResBean.get(Long.parseLong(resid)).childrenByType(Long.parseLong(childType));
            if (beans.length > 0) {
                resid = beans[0].res().getId() + "";
            } else {
                resid = "-1";
                return null;
            }
        }

        Map<String, JSONArray> tblMap = new HashMap<String, JSONArray>();

        //datatbles的表头 
        JSONArray columns = new JSONArray();
        //datatables的数据
        JSONArray datas = new JSONArray();

        JSONArray props = new JSONArray();
        Property property = PropertyBean.get(Long.parseLong(propId)).property();
        if (property.getDataTypeId().intValue() == 4 && property.getFormat() != null) {
            props.add(property.getName());

            String format = property.getFormat();
            String[] formatArray = format.split("[|]");
            JSONArray col = new JSONArray();
            for (int r = 0; r < formatArray.length; r++) {
                String colValue = formatArray[r];
                col.add(colValue);
            }
            columns.add(col);

            PropValueBean valueBean = cache.getValueBean(Long.parseLong(resid), property.getId().longValue());
            if (valueBean != null) {
                String[] values = valueBean.getValue().split("<#>");
                JSONArray row = new JSONArray();
                for (int v = 0; v < values.length; v++) {
                    String[] dataArray = values[v].split("<@>");
                    JSONArray column = new JSONArray();
                    for (int d = 0; d < dataArray.length; d++) {
                        column.add(dataArray[d]);
                    }
                    row.add(column);
                }
                datas.add(row);
            }
        }
        //todo test
        //System.out.println("columns==\n" + columns.toString());
        tblMap.put("columns", columns);
        tblMap.put("data", datas);
        tblMap.put("props", props);
        Struts2Utils.renderJson(tblMap);
        return null;
    }

    /**
     * 生成一个资源下同一个资源类别资源的特定指标的柱图  X:资源 Y:指标值
     * @return
     */
    public String propColumn() {
        String resId = request.getParameter("resId");
        String childType = request.getParameter("childType");
        String propIds = request.getParameter("propIds");
        String detail = request.getParameter("detail");
        ResBean[] resBean;
        try {
            if (childType != null && childType.length() > 0) {
                resBean = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
            } else {
                ResBean rb = ResBean.get(Long.parseLong(resId));
                resBean = new ResBean[1];
                resBean[0] = rb;
            }

            String[] tmp = propIds.split(",");
            JSONObject json = buildJSONForColumn(detail, resBean, tmp);

            Struts2Utils.renderText(json.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 为柱状图图表构建数据  stacked 
     * @return
     */
    private JSONObject buildJSONForColumn(String detail, ResBean[] resBean, String[] propIds) {
        JSONObject json = new JSONObject();
        JSONObject chart = new JSONObject();
        chart.element("canvasbgalpha", "0");
        chart.element("bgAlpha", "0");
        chart.element("legendPosition", "RIGHT");
        chart.element("showPlotBorder", "1");
        chart.element("showValues", "1");
        chart.element("canvasBorderThickness", "1");
        chart.element("numVDivLines", "10");
        chart.element("bgcolor", "FFFFFF");
        chart.element("bordercolor", "FFFFFF");
        chart.element("showlabels", "1");
        chart.element("showShadow", "1");
        chart.element("showlegend", "0");
        chart.element("useRoundEdges", "1");
        chart.element("showVLineLabelBorder", "1");
        chart.element("showBorder", "1");

        if ("1".equals(detail)) {
            chart.element("numbersuffix", "%");
        }
        JSONArray categorie = new JSONArray();
        for (ResBean rb : resBean) {
            JSONObject j = new JSONObject();
            j.element("label", rb.res().getName());
            categorie.add(j);
        }

        JSONArray dataSet = new JSONArray();
        for (String propId : propIds) {
            JSONObject obj = new JSONObject();
            JSONArray jarr = new JSONArray();
            for (ResBean bean : resBean) {
                JSONObject o = new JSONObject();
                o.element("value", propertyValueCache.getValue(bean.res().getId(), Long.parseLong(propId)));
                jarr.add(o);
            }
            obj.element("data", jarr);
            obj.element("seriesname", PropertyBean.get(Long.parseLong(propId)).property().getName());
            dataSet.add(obj);
        }
        JSONObject o = new JSONObject();
        o.element("category", categorie);
        JSONArray jj = new JSONArray();
        jj.add(o);
        json.element("categories", jj);
        json.element("dataset", dataSet);
        json.element("chart", chart);
        return json;
    }

    /**
     * 返回一个资源下的某个资源类别的子资源  
     * @return
     */
    public String childResList() {
        String resId = request.getParameter("resId");
        String childType = request.getParameter("childType");
        JSONObject json = new JSONObject();
        JSONArray jarry = new JSONArray();
        ResBean[] resBeans = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
        for (ResBean rb : resBeans) {
            JSONObject obj = new JSONObject();
            obj.element("id", rb.res().getId());
            obj.element("name", rb.res().getName());
            jarry.add(obj);
        }
        json.element("res", jarry);

        Struts2Utils.renderText(json.toString());
        return null;
    }

    /**
     * 获取如日志空间占用 类似的水平占用比数据
     * @return
     */
    public String getHLedPercent() {

        JSONObject json = new JSONObject();
        String resId = request.getParameter("resId");
        String childType = request.getParameter("childType");
        String maxPropId = request.getParameter("maxPropId");
        String usePropId = request.getParameter("usePropId");
        try {
            ResBean rbean = null;
            if (childType != null && childType.length() > 0) {
                ResBean[] beans = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
                if (beans.length > 0) {
                    rbean = ResBean.get(beans[0].res().getId());
                } else {
                    return null;
                }
            }
            String maxVal = propertyValueCache.getValue(rbean.res().getId(), Long.parseLong(maxPropId));
            String useVal = propertyValueCache.getValue(rbean.res().getId(), Long.parseLong(usePropId));

            json = buiidJsonForHLed(maxVal, useVal);

        } catch (Exception e) {
            logger.error(e);
        }

        Struts2Utils.renderText(json.toString());
        return null;
    }

    /**
     * 为水平占用比图构建json对象
     * @param maxVal
     * @param useVal
     * @return
     */
    private JSONObject buiidJsonForHLed(String maxVal, String useVal) {
        JSONObject json = new JSONObject();
        JSONObject chart = new JSONObject();
        chart.element("manageresize", "1");
        chart.element("lowerlimit", "0");
        chart.element("upperlimit", maxVal);
        chart.element("upperlimitdisplay", "总大小");
        chart.element("palette", "4");
        chart.element("numbersuffix", "B");
        //        chart.element("chartrightmargin", "20");
        //        chart.element("chartleftmargin", "20");
        chart.element("ledsize", "5");
        chart.element("ledgap", "5");
        chart.element("ticksbelowgauge", "0");
        chart.element("decimals", "0");
        chart.element("bgColor", "FFFFFF");
        chart.element("baseFontColor", "000000");

        JSONObject colorrange = new JSONObject();
        JSONArray color = new JSONArray();
        JSONObject color1 = new JSONObject();
        color1.element("minvalue", "0");
        color1.element("maxvalue", maxVal);
        color1.element("code", "00FF00");
        color.add(color1);
        colorrange.element("color", color);

        json.element("chart", chart);
        json.element("value", useVal);
        json.element("colorrange", colorrange);
        return json;
    }

    /**
     * 
     * @return
     */
    public String getHLineGauge() {
        JSONObject json = new JSONObject();
        String resId = request.getParameter("resId");
        String childType = request.getParameter("childType");
        String maxPropId = request.getParameter("maxPropId");
        String usePropId = request.getParameter("usePropId");
        String unit = request.getParameter("unit");
        try {
            ResBean rbean = null;
            if (childType != null && childType.length() > 0) {
                ResBean[] beans = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
                if (beans.length > 0) {
                    rbean = ResBean.get(beans[0].res().getId());
                } else {
                    return null;
                }
            }
            String maxVal = "";
            if ("100".equals(maxPropId)) {
                maxVal = "100";
            } else {
                maxVal = propertyValueCache.getValue(rbean.res().getId(), Long.parseLong(maxPropId));
            }
            String useVal = propertyValueCache.getValue(rbean.res().getId(), Long.parseLong(usePropId));

            json = buildJSONForLine(maxVal, useVal, unit);

        } catch (Exception e) {
            logger.error(e);
        }

        Struts2Utils.renderText(json.toString());

        return null;
    }

    /**
     * 水平百分比 json对象
     * @return
     */
    private JSONObject buildJSONForLine(String maxVal, String minVal, String unit) {
        JSONObject json = new JSONObject();
        JSONObject chat = new JSONObject();
        JSONObject colorrange = new JSONObject();
        JSONArray cory = new JSONArray();
        chat.element("manageresize", "1");
        chat.element("bgcolor", "FFFFFF");
        chat.element("bgalpha", "0");
        //        chat.element("showborder", "0");
        chat.element("lowerlimit", "0");
        //        chat.element("gaugeroundradius", "5");
        //        chat.element("chartbottommargin", "10");
        //        chat.element("ticksbelowgauge", "0");
        //        chat.element("showgaugelabels", "1");
        //        chat.element("valueabovepointer", "0");
        //        chat.element("pointerontop", "1");
        //        chat.element("pointerradius", "9");
        chat.element("numbersuffix", unit);

        maxVal = maxVal == null ? "100" : maxVal;
        minVal = minVal == null ? "0" : minVal;
        chat.element("upperlimit", maxVal);

        if ("%".equals(unit)) {
            JSONObject co1 = new JSONObject();
            co1.element("minvalue", "0");
            co1.element("maxvalue", "35");
            co1.element("code", "eeeeee");
            co1.element("label", "低");
            cory.add(co1);

            JSONObject co2 = new JSONObject();
            co2.element("minvalue", "35");
            co2.element("maxvalue", "70");
            co2.element("code", "888888");
            co2.element("label", "中");
            cory.add(co2);

            JSONObject co3 = new JSONObject();
            co3.element("minvalue", "70");
            co3.element("maxvalue", "100");
            co3.element("code", "333333 ");
            co3.element("label", "高");
            cory.add(co3);
        } else {
            JSONObject co1 = new JSONObject();
            co1.element("minvalue", "0");
            co1.element("code", "eeeeee");
            co1.element("maxvalue", Long.parseLong(maxVal) / 3.0);
            co1.element("label", "低");
            cory.add(co1);

            JSONObject co2 = new JSONObject();
            co2.element("minvalue", "35");
            co2.element("maxvalue", Long.parseLong(maxVal) * 2 / 3.0);
            co2.element("code", "888888");
            co2.element("label", "中");
            cory.add(co2);

            JSONObject co3 = new JSONObject();
            co3.element("minvalue", "70");
            co3.element("maxvalue", maxVal);
            co3.element("code", "333333 ");
            co3.element("label", "高");
            cory.add(co3);
        }
        colorrange.element("color", cory);
        JSONObject pointers = new JSONObject();
        JSONArray pointer = new JSONArray();
        JSONObject val = new JSONObject();
        val.element("value", minVal);
        pointer.add(val);
        pointers.element("pointer", pointer);

        json.element("pointers", pointers);
        json.element("colorrange", colorrange);
        json.element("chart", chat);
        return json;
    }

    /**
     * 获取一个资源下的特定类别子资源信息
     * @return
     */
    public String getChildResInfo() {
        String resId = request.getParameter("resid");
        String childType = request.getParameter("childType");
        String valueIds = request.getParameter("valueid");
        String[] tmp = valueIds.split(",");

        // System.out.println("resId=" + resId + "childType=" + childType + "valueIds=" + valueIds);
        JSONArray childRes = new JSONArray();
        JSONObject json = new JSONObject();
        ResBean[] resBean = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
        for (ResBean b : resBean) {
            JSONObject obj = new JSONObject();
            obj.element("name", b.res().getName());
            obj.element("ip", b.res().getIp());
            for (String t : tmp) {
                obj.element(t, getValue(b.res().getId(), t));
            }
            childRes.add(obj);
        }
        json.element("aaData", childRes);
        json.element("iTotalRecords", 0);
        json.element("iTotalDisplayRecords", 0);
        Struts2Utils.renderText(json.toString());
        return null;
    }

    /**
     * 获取一个资源的属性事 指标值  attr_ 表示属性  prop_ 表示指标
     * @param id
     * @param prop
     * @return
     */
    private String getValue(Long id, String prop) {
        if (prop == null || prop.length() == 0) {
            return "";
        }
        if (prop.contains("prop")) {
            String val = propertyValueCache.getValue(id, Long.parseLong(prop.split("_")[1]));
            return val == null ? "" : val;
        }
        if (prop.contains("attr")) {
            ResAttrVal rav = resAttrValCache.get(id, Long.parseLong(prop.split("_")[1]));
            return rav == null ? "" : rav.getAttrValue();
        } else {
            return "";
        }
    }

    /**
     * 获取历史指标列表
     * @return
     */
    public String getHisPropsList() {
        String resId = request.getParameter("resId");
        String childType = request.getParameter("childType");
        String props = request.getParameter("propIds");

        JSONObject json = new JSONObject();
        JSONArray cols = new JSONArray();
        ResBean rbean = null;
        if (childType != null && childType.length() > 0) {
            ResBean[] beans = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
            if (beans.length > 0) {
                rbean = ResBean.get(beans[0].res().getId());
            } else {
                return null;
            }
        }

        String[] propArry = null;
        if (props != null && props.length() > 0) {
            propArry = props.split(",");
        }
        int[] propids = new int[propArry.length];
        for (int i = 0; i < propArry.length; i++) {
            Property p = propertyCache.get(Long.parseLong(propArry[i]));
            int ii = p.getStore();
            propids[i] = ii;
            JSONObject c = new JSONObject();
            c.element("name", p.getName());
            cols.add(c);
        }

        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        Calendar now = Calendar.getInstance();
        String startDate = sf.format(now.getTime()) + "000000";

        List<PropertyValue> list = propService.getPropValues(rbean.res().getId(), propids, startDate, "", "0");
        JSONArray propArray = new JSONArray();

        for (PropertyValue pv : list) {
            JSONArray ay = new JSONArray();
            String d = pv.getTime().toString();
            ay.add(d.substring(0, d.toString().length() - 2));
            for (int i = 0; i < propids.length; i++) {
                String val = pv.getValues()[i];
                ay.add(val == null ? "&nbsp;" : val);
            }
            propArray.add(ay);
        }

        json.element("cols", cols);
        json.element("data", propArray);
        Struts2Utils.renderText(json.toString());
        return null;
    }

    /**
     * 获取一类子资源的列表
     * @return
     */
    public String getListFromCache() {
        SimpleDateFormat ff = new SimpleDateFormat("HH:mm:ss yyyy-MM-dd");
        String resId = request.getParameter("resId");
        String childType = request.getParameter("childType");
        String props = request.getParameter("propIds");

        JSONObject json = new JSONObject();
        JSONArray cols = new JSONArray();
        ResBean[] beans = null;
        if (childType != null && childType.length() > 0) {
            beans = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
        } else {

        }

        String[] propArry = null;
        if (props != null && props.length() > 0) {
            propArry = props.split(",");
        }

        for (int i = 0; i < propArry.length; i++) {
            Property p = propertyCache.get(Long.parseLong(propArry[i]));
            JSONObject c = new JSONObject();
            c.element("name", p.getName());
            cols.add(c);
        }

        JSONArray propArray = new JSONArray();

        for (ResBean rb : beans) {
            JSONArray ay = new JSONArray();

            for (int i = 0; i < propArry.length; i++) {
                PropValueBean bean = propertyValueCache.getValueBean(rb.res().getId(), Long.parseLong(propArry[i]));
                if (bean == null) {
                    continue;
                }
                if (i == 0) {
                    ay.add(ff.format(bean.getTime()));
                    ay.add(rb.res().getName());
                }
                ay.add(bean.getValue() == null ? "&nbsp;" : bean.getValue());
            }
            propArray.add(ay);
        }

        json.element("cols", cols);
        json.element("data", propArray);
        Struts2Utils.renderText(json.toString());
        return null;
    }

    //#########################双层子资源#############################
    public String childResListForGrand() {
        String resId = request.getParameter("resId");
        String childType = request.getParameter("childType");
        String grandchildType = request.getParameter("grandchildType");
        JSONObject json = new JSONObject();
        JSONArray jarry = new JSONArray();
        ResBean childBean = null;
        ResBean[] resBeans = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
        if (resBeans != null && resBeans.length > 0) {
            childBean = resBeans[0];
        } else {
            return null;
        }

        ResBean[] grandBeans = childBean.childrenByType(Long.parseLong(grandchildType));
        for (ResBean rb : grandBeans) {
            JSONObject obj = new JSONObject();
            obj.element("id", rb.res().getId());
            obj.element("name", rb.res().getName());
            jarry.add(obj);
        }
        json.element("res", jarry);

        Struts2Utils.renderText(json.toString());
        return null;
    }

    /**
     * 
     * @return
     */
    public String get2ColumnChartForGrand() {
        String resId = request.getParameter("resId");
        String childType = request.getParameter("childType");
        String grandchildType = request.getParameter("grandchildType");
        String propId = request.getParameter("propIds");
        //String detail = request.getParameter("detail");
        String top = request.getParameter("top");
        String unit = request.getParameter("unit");

        ResBean[] childBeans = null;
        ResBean childBean = null;
        ResBean[] resBean;
        try {
            if (childType != null && childType.length() > 0) {
                childBeans = ResBean.get(Long.parseLong(resId)).childrenByType(Long.parseLong(childType));
                if (childBeans != null && childBeans.length > 0) {
                    childBean = childBeans[0];
                } else {
                    return null;
                }
            } else {
                return null;
            }

            if (grandchildType != null && grandchildType.length() > 0) {
                resBean = childBean.childrenByType(Long.parseLong(grandchildType));
            } else {
                resBean = new ResBean[1];
                resBean[0] = childBean;
            }
            JSONObject json = buildJsonFor2Column(unit, resBean, propId, Integer.parseInt(top));

            Struts2Utils.renderText(json.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 获取指标原始值 不进行FusionCharts的参数封装
     */
    public void getPropValues() {
        Long resId = Long.parseLong(request.getParameter("resId"));
        String dimen = request.getParameter("dimen");
        String trendtype = request.getParameter("trendtype");
        String propIds = request.getParameter("propIds");
        String timeSection = request.getParameter("timeSection");
        String beginDate = request.getParameter("beginDate");
        String endDate = request.getParameter("endDate");
        String[] props = propIds.split("[,]");
        int[] propids = new int[props.length];
        for (int i = 0; i < props.length; i++) {
            Property p = propertyCache.get(Long.parseLong(props[i]));
            int ii = p.getStore();
            propids[i] = ii;
        }

        List<PropertyValue> propValues = propService.getTrendPropValus(resId, propids, DateGeneric.genericStartDate(timeSection, beginDate), DateGeneric.getnericEndDate(dimen, timeSection, endDate), trendtype, dimen);
        Struts2Utils.renderJson(propValues);
    }

    //############################END#####################################

    @Override
    public Object getModel() {
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    @Override
    public String save() throws Exception {
        return null;
    }

    @Override
    public String delete() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String list() throws Exception {
        return null;
    }

    /**
     * 
     * <p>
     * Description: 用于排序的对象类
     * </p>
     *
     * @since 
     * @see
     */
    class FileSystem {
        private final String name;
        private final Double value;

        public FileSystem(String name, Double value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public Double getValue() {
            return value;
        }

    }

    /**
     * 自定一个排序算法 按资源类别id升序排序  这样能保证主机资源会排在第一位
     *
     */
    class FSSort implements Comparator {
        @Override
        public int compare(Object obj1, Object obj2) {
            FileSystem res1 = (FileSystem) obj1;
            FileSystem res2 = (FileSystem) obj2;
            if (res1.getValue() > res2.getValue())
                return 0;
            else
                return 1;
        }
    }

}
